package com.lzm.ydpt.view.pop;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.byt.framlib.R;
import com.byt.framlib.commonwidget.OnNoDoubleClickListener;


public class ContactPopWindows extends PopupWindow {
    private Context mContext;
    private TextView textView01;
    private TextView textView02;
    private Button cancalBtn;
    private ListViewOnImtermClick mListViewOnImtermClick;


    /**
     *
     * @param context
     * @param width
     * @param height
     */
    @SuppressLint("NewApi")
    public ContactPopWindows(Context context, int width, int height, ListViewOnImtermClick listViewOnImtermClick) {
        super();
        this.mContext = context;
        this.mListViewOnImtermClick = listViewOnImtermClick;
        setFocusable(true);
        // 设置弹窗内可点击
        setTouchable(true);
        // 设置弹窗外可点击
        setOutsideTouchable(true);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        // this.setAnimationStyle(R.style.mystyle);
        // 加上这个只是为了让返回键点击能消失popWindow
        setBackgroundDrawable(new BitmapDrawable());
        // 设置弹窗的宽度和高度
        setWidth(width);
        setHeight(height);
        // 设置弹窗的布局界面
        setContentView(LayoutInflater.from(mContext).inflate(R.layout.popwindow_list, null));
        initUi();
    }

    private void initUi() {
        textView01 = getContentView().findViewById(R.id.phone_01_tv);
        textView02 = getContentView().findViewById(R.id.phone_02_tv);
        cancalBtn = getContentView().findViewById(R.id.btn_cancal);
        cancalBtn.setOnClickListener(v -> dismiss());
        textView01.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (mListViewOnImtermClick != null) {
                    mListViewOnImtermClick.onLvOnItem(textView01.getText().toString());
                }
                dismiss();
            }
        });
        textView02.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (mListViewOnImtermClick != null) {
                    mListViewOnImtermClick.onLvOnItem(textView02.getText().toString());
                }
                dismiss();
            }
        });
    }

    public void setContactService(String phone1, String phone2) {
        textView01.setText(phone1);
        textView02.setText(phone2);
    }

    public void showPop(View v) {
        showAtLocation(v, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); // 设置layout在PopupWindow中显示的位置
    }

    public interface ListViewOnImtermClick {
        void onLvOnItem(String title);
    }
}